package com.pai4j.aigc.controller;

import com.pai4j.aigc.service.UnreadChatSummaryService;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/aigc-service/unread-summary")
public class UnreadSummaryController {

    @Autowired
    private UnreadChatSummaryService unreadChatSummaryService;

    /**
     * 生成未读消息摘要预览（支持Bot插件调用）
     *
     * @param account 当前用户ID
     * @param maxConvs 最大会话数（默认5，最大20）
     * @param maxMsgsPerConv 每会话抽样消息数（默认5，最大20）
     * @return 返回摘要文本，供Bot插件进行LLM增强
     */
    @PostMapping("/preview")
    public PAIResponseBean<String> preview(@RequestParam @NotBlank String account,
                                           @RequestParam(required = false) Integer maxConvs,
                                           @RequestParam(required = false) Integer maxMsgsPerConv) {
        try {
            log.info("【未读消息汇总插件】开始处理，account:{}, maxConvs:{}, maxMsgsPerConv:{}", account, maxConvs, maxMsgsPerConv);
            String summary = unreadChatSummaryService.summarizeUnread(account, maxConvs, maxMsgsPerConv);
            log.info("【未读消息汇总插件】处理完成，account:{}, 摘要长度:{}", account, summary != null ? summary.length() : 0);
            return PAIResponseBeanUtil.success(summary);
        } catch (Exception e) {
            log.error("【未读消息汇总插件】处理异常，account:" + account, e);
            return PAIResponseBeanUtil.fail("未读消息汇总失败：" + e.getMessage());
        }
    }
}
